//
//  LZMyMessageCenterCell.m
//  laziz_Merchant
//
//  Created by xbnzd on 17/3/1.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZMyMessageCenterCell.h"
#import "LZDateFormatterManager.h"
#import "LZMessageModel.h"
#import "UIImageView+WebCache.h"

@interface LZMyMessageCenterCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIView *pointView;

@end

@implementation LZMyMessageCenterCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.contentLabel.preferredMaxLayoutWidth = SCREEN_WIDTH - 20 - 20 - 44;
    self.iconView.layer.cornerRadius = 22.0;
    self.iconView.layer.masksToBounds = YES;
    self.iconView.backgroundColor = [UIColor yellowColor];
    self.pointView.layer.cornerRadius = 4.;
    self.pointView.layer.masksToBounds = YES;
    self.pointView.backgroundColor = [UIColor redColor];
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    for (UIView *subView in self.subviews) {
        
        //只有滑动时才会创建 UITableViewCellDeleteConfirmationView
        if([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
            UIView *deleteConfirmationView = subView.subviews[0];
            //改背景颜色
            deleteConfirmationView.backgroundColor = [UIColor clearColor];
            self.deleteImgView.frame = CGRectMake(0, 0, 42, 42);
            if (self.deleteImgView.hidden == YES) {
                [self performSelector:@selector(showDeleteImg) withObject:nil afterDelay:0.5];
            }
            
            [deleteConfirmationView addSubview:self.deleteImgView];
            
            self.deleteImgView.center = CGPointMake(deleteConfirmationView.center.x, deleteConfirmationView.center.y);
        }
    }
}


/**
 加延时的目的是为了防止用户删除后,重用的cell依然隐藏了删除按钮. 但如果直接设置不隐藏.则删除动画按钮会上移.很难看
 */
- (void)showDeleteImg
{
    self.deleteImgView.hidden = NO;
}


- (void)loadContentWithModel:(LZMessageModel *)msgModel
{
    self.timeLabel.text = [[LZDateFormatterManager shareInstance] stringFromDoubleTimestampToyyyyLineMMLinedd:(msgModel.msgTime)];
    
    if (msgModel.isRead == 0) {
        self.pointView.hidden = NO;
    }else{
        self.pointView.hidden = YES;
    }
    
    switch (msgModel.msgType) {
        case kMsgCenterType_customerResComment:{//餐厅评论
            [self.iconView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",APIBaseURL,msgModel.sellerPhoto]] placeholderImage:[UIImage imageNamed:@"msg_sys"]];
            self.userNameLabel.text = msgModel.restaurantName;
            self.contentLabel.text = LZLocalizedString(@"message_new_review", nil);
        }
            break;
        case kMsgCenterType_customerVideoComment:{//视频评论
            [self.iconView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",APIBaseURL,msgModel.sellerPhoto]] placeholderImage:[UIImage imageNamed:@"msg_sys"]];
            self.userNameLabel.text = msgModel.restaurantName;
            self.contentLabel.text = LZLocalizedString(@"message_new_review", nil);
        }
            break;
        case kMsgCenterType_couponExpired:{//优惠券过期提醒
            self.iconView.image = [UIImage imageNamed:@"msg_sys"];
            self.userNameLabel.text = LZLocalizedString(@"message_system", nil);
            self.contentLabel.text = LZLocalizedString(@"message_coupon_expire", nil);
        }
            break;
        case kMsgCenterType_groupExpired:{//团购券过期提醒
            self.iconView.image = [UIImage imageNamed:@"msg_sys"];
            self.userNameLabel.text = LZLocalizedString(@"message_system", nil);
            self.contentLabel.text = LZLocalizedString(@"message_coupon_expire", nil);
        }
            break;
        case kMsgCenterType_groupSuccess:{//成团提醒
            self.iconView.image = [UIImage imageNamed:@"msg_sys"];
            self.userNameLabel.text = LZLocalizedString(@"message_system", nil);
            self.contentLabel.text = LZLocalizedString(@"message_goupon_success", nil);
        }
            break;
            
        default:
            break;
    }
}


+ (CGFloat)getCellHeightWithText:(NSString *)text
{
    if (text) {
        NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:13.]};
        CGRect titleRect = [text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-74 - 20, MAXFLOAT)
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:attributes
                                              context:nil];
        return 25 + 18 + 10 +titleRect.size.height + 20;
    }
    else {
        return 25+ 18 + 10 + 20 + 20;
    }
}

- (UIImageView *)deleteImgView
{
    if (_deleteImgView) {
        return _deleteImgView;
    }
    
    _deleteImgView = [[UIImageView alloc] init];
    _deleteImgView.contentMode = UIViewContentModeScaleAspectFit;
    _deleteImgView.image = [UIImage imageNamed:@"staff_delete"];
    
    return _deleteImgView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
